package com.mxx.pmxt.service.impl;

import com.mxx.pmxt.dao.GoodsModelMapper;
import com.mxx.pmxt.dao.PUsersModelMapper;
import com.mxx.pmxt.dao.UserGoodsModelMapper;
import com.mxx.pmxt.model.GoodsModel;
import com.mxx.pmxt.model.PUsersModel;
import com.mxx.pmxt.model.UserGoodsModel;
import com.mxx.pmxt.model.pojo.AfterCheckByUserModel;
import com.mxx.pmxt.service.GoodsService;
import com.mxx.pmxt.service.PUserService;
import com.mxx.pmxt.service.UserGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 马潇霄 on 2017/4/24.
 */
@Service
public class UserGoodsServiceImpl implements UserGoodsService
{
    @Resource
    UserGoodsModelMapper userGoodsModelMapper;

    @Resource
    GoodsModelMapper goodsModelMapper;

    @Resource
    PUsersModelMapper pUsersModelMapper;



    public BigDecimal maxPriceByGid(int gid)
    {
        return userGoodsModelMapper.selectMaxPriceByGid(gid);
    }

    public int putMoney(int gid, String uname,BigDecimal newprice)
    {
        PUsersModel pUsersModel = pUsersModelMapper.getUserByName(uname).get(0);
        BigDecimal account = pUsersModel.getUaccount();
        BigDecimal maxPrice = userGoodsModelMapper.selectMaxPriceByGid(gid);
        BigDecimal  maxUserPrice = userGoodsModelMapper.selectMaxPriceByGidUser(gid,uname);

        if (account.compareTo(newprice)==-1){
            return -1;//余额不足
        }
        if (newprice.compareTo(maxPrice)==-1){
            return -2;//加价小于当前价格
        }
        GoodsModel goodsModel = goodsModelMapper.selectByPrimaryKey(gid);
        if (goodsModel.getGstate()!=1){
            return -3;//状态不是拍卖中
        }
        if (uname.equals(goodsModel.getGowner())){
            return -5;//不能给自己加价
        }
        if(maxUserPrice==null){
            //第一次加价
            account=account.subtract(newprice);
        }else {
            //不是第一次加价
            if (newprice.compareTo(maxUserPrice)==-1){
                //出价小于上一次出价
                return -4;
            }
            account=account.subtract(newprice.subtract(maxUserPrice));
            //账户减掉差价
        }
        pUsersModel.setUaccount(account);
        pUsersModelMapper.updateByPrimaryKeySelective(pUsersModel);//从账户扣钱
        UserGoodsModel userGoodsModel = new UserGoodsModel();
        userGoodsModel.setPnowtime(new Date());
        userGoodsModel.setPisget(0);
        userGoodsModel.setPgoodid(gid);
        userGoodsModel.setPuser(uname);
        userGoodsModel.setPnowprice(newprice);
        userGoodsModelMapper.insert(userGoodsModel);
        return 1;
    }

    public List<AfterCheckByUserModel> selectIsGetByUserByPage(int startRow, int pageSize, String uname)
    {
        List<UserGoodsModel> userGoodsModels = userGoodsModelMapper.selectIsGetByUserByPage(startRow,pageSize,uname);
        List<AfterCheckByUserModel> afterCheck = new ArrayList<AfterCheckByUserModel>();
        for (UserGoodsModel userGoodsModel:userGoodsModels)
        {
            int gid = userGoodsModel.getPgoodid();
            GoodsModel goodModel = goodsModelMapper.selectByPrimaryKey(gid);
            AfterCheckByUserModel afterCheckByUserModel = new AfterCheckByUserModel();
            afterCheckByUserModel.setGid(goodModel.getGid());
            afterCheckByUserModel.setGpicture(goodModel.getGpicture());
            afterCheckByUserModel.setGname(goodModel.getGname());
            afterCheckByUserModel.setGprice(goodModel.getGprice());
            afterCheckByUserModel.setNowprice(userGoodsModel.getPnowprice());
            afterCheckByUserModel.setGdetail(goodModel.getGdetail());
            afterCheckByUserModel.setGkindid(goodModel.getGkindid());
            afterCheckByUserModel.setGstarttime(goodModel.getGstarttime());
            afterCheckByUserModel.setGendtime(goodModel.getGendtime());
            afterCheck.add(afterCheckByUserModel);
        }
        return afterCheck;
    }

    public int countSelectIsGetByUserByPage(String uname)
    {
        return userGoodsModelMapper.countSelectIsGetByUserByPage(uname);
    }
}
